package com.shuaige.paydollarapi.paydoolar.secure;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

public class SHAPaydollarSecure implements PaydollarSecure {

    private SHAAlgorithmUtil algorithmUtil;

    public SHAPaydollarSecure() {
        algorithmUtil = new SHAAlgorithmUtil();
    }

    @Override
    public String generatePaymentSecureHash(String merchantId,
                                            String merchantReferenceNumber, String currencyCode, String amount,
                                            String paymentType, String secureHashSecret)
            throws PaydollarSecureException {

        StringBuffer buffer = new StringBuffer();
        buffer.append(merchantId).append("|").append(merchantReferenceNumber)
                .append("|").append(currencyCode).append("|").append(amount)
                .append("|").append(paymentType).append("|").append(
                secureHashSecret);

        try {

            return algorithmUtil.operationAlgorithm(buffer.toString());

        } catch (NoSuchAlgorithmException e) {
            throw new PaydollarSecureException(e);
        } catch (UnsupportedEncodingException e) {
            throw new PaydollarSecureException(e);
        } finally {
            buffer.delete(0, buffer.length());
        }

    }

    @Override
    public boolean verifyPaymentDatafeed(String src, String prc,
                                         String successCode, String merchantReferenceNumber,
                                         String payDollarReferenceNumber, String currencyCode,
                                         String amount, String payerAuthenticationStatus,
                                         String secureHashSecret, String secureHash) {

        StringBuffer buffer = new StringBuffer();
        buffer.append(src).append("|").append(prc).append("|").append(
                successCode).append("|").append(merchantReferenceNumber)
                .append("|").append(payDollarReferenceNumber).append("|")
                .append(currencyCode).append("|").append(amount).append("|")
                .append(payerAuthenticationStatus).append("|").append(
                secureHashSecret);

        try {


            String verifyData = algorithmUtil.operationAlgorithm(buffer
                    .toString());
            System.out.println("****************verify Begin**************");
            System.out.println("secureHash=[" + secureHash + "]");
            System.out.println("ExpectedSecureHash=[" + verifyData + "]");
            System.out.println("****************verify end**************");
            if (secureHash.equals(verifyData))
                return true;
            else
                return false;
        } catch (Exception e) {
            return false;
        } finally {
            buffer.delete(0, buffer.length());
        }

    }

    @Override
    public String loadSecureHashSecret() throws NullPointerException {
        return "n2vhiQJJAFjcPhLRra1p8Ru9TWRYoTDv";
    }

}
